package common.matchAward.view
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.greensock.easing.Quad;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.MatchEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.match.vo.MatchEndAwardVo;
	
	import common.item.ComGoodsItem;
	import common.matchAward.IMatchAwardView;
	import common.matchAward.item.MatchAwardItem;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import lz.controls.CompGlobalDef;
	import lz.controls.core.TitleWindow;
	import lz.controls.core.UIComponent;
	import lz.controls.managers.AnimationManager;

	/**
	 *赛后抽奖 
	 * @author lake
	 * 
	 */	
	public class MatchAwardView extends TitleWindow implements IMatchAwardView
	{
		/**
		 *加载器 
		 */		
		private var loader 		:QueueLoader;
		/**
		 *确认 
		 */		
		private var sureBtn		:SimpleButton;
		/**
		 * 显示的4个奖励
		 */		
		private var itemList		:Array;
		/**
		 *开箱动画mc 
		 */		
		private var openBoxMc		:MovieClip;
		/**
		 * 开箱item 
		 */		
		private var openBoxItem :MatchAwardItem;
		/**
		 *数据 
		 */		
		private var _data :*;
		/**
		 *第几个 
		 */		
		private var type :int;
		
		public function MatchAwardView()
		{
			super();
		}
		/**
		 *设置数据 
		 * @param value
		 * 
		 */		
		public function set data(value:*):void
		{
			_data = value;
		}
		/**
		 *获取数据 
		 * @return 
		 * 
		 */		
		public function get data():*
		{
			return null;
		}
		/**
		 *初始化 
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.lz.res.match.SelectAwardSkin");
			if(_symbolSkin)
			{
				config();
			}
			else
			{
				loader = new QueueLoader();
				loader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,loadCompleteHandler);
				loader.addQueue(new QueueVo("match_award"),"res/swf/match/Match_Award_1.swf?" + GlobalDef.ReleaseVersion + ".swf");
				loader.queueLoadStart();
			}
		}
		/**
		 * loaderErrorHandler
		 * @param e
		 * 
		 */		
		private function loaderErrorHandler(e :Event):void
		{
			
		}
		/**
		 * 
		 * 加载完毕
		 */		
		private function loadCompleteHandler(e :Event):void
		{
			_symbolSkin = Reflection.createInstance("com.lz.res.match.SelectAwardSkin");
			if(_symbolSkin)
			{
				config();
			}
			else
			{
				destroyView();
			}
			loader.removeEventListener(QueueLoaderEvent.QUEUE_COMPLETE,loadCompleteHandler);
			loader = null;
		}
		/**
		 *配置界面 
		 * 
		 */		
		private function config():void
		{
			this.addChild(_symbolSkin);
			sureBtn = _symbolSkin.sureBtn;
			_symbolSkin.gotoAndPlay(1);
			_symbolSkin.x = (GlobalDef.GameStage.stageWidth - 690)/2;
			_symbolSkin.y = (GlobalDef.GameStage.stageHeight - 267)/2;
			_symbolSkin.mouseChildren = false;
			_symbolSkin.mouseEnabled = false;
			_symbolSkin.addFrameScript(9,enableSk);
			itemList = [];
			var awardItem :MatchAwardItem;
			var i :int = 0;
			while(i < 4)
			{
				awardItem = new MatchAwardItem();
				awardItem.initUI(_symbolSkin["item_" + i]);
				//_symbolSkin["item_" + i].visible = true;
				//_symbolSkin["item_" + i].alpha = 1;
				itemList.push(awardItem);
				i++;
			}
			addEvtListener();
			//自适应
			resizeEnabled = true;
			sureBtn.visible = false;
		}
		/**
		 *可点 
		 */		
		private function enableSk():void
		{
			_symbolSkin.mouseChildren = true;
			_symbolSkin.mouseEnabled = true;
		}
		/**
		 *添加事件 
		 * 
		 */		
		public function addEvtListener():void
		{
			this.addEventListener(MouseEvent.CLICK,onBoxItemClickHandler);
			sureBtn.addEventListener(MouseEvent.CLICK,sureHandler);
		}
		/**
		 *自适应 
		 * @param e
		 * 
		 */		
		override protected function onResizeHandler(e :Event):void
		{
			_symbolSkin.x = (GlobalDef.GameStage.stageWidth - 690)/2;
			_symbolSkin.y = (GlobalDef.GameStage.stageHeight - 267)/2;
		}
		/**
		 *箱子点击 
		 * @param e
		 */		
		private function onBoxItemClickHandler(e :MouseEvent):void
		{
			var targetName :String = String(e.target.name);
			if(targetName.indexOf("item_") == -1)
			{
				return ;
			}
			var i :int = 0;
			while(i < 4)
			{
				_symbolSkin["item_" + i].visible = true;
				_symbolSkin["item_" + i].alpha = 1;
				i++;
			}
			type = int(targetName.split("_")[1]);
			_symbolSkin["item_" + type].visible = false;
			openBoxMc = Reflection.createInstance("com.lz.res.match.OpenBoxSkin");
			if(openBoxMc == null)
			{
				return;
			}
			_symbolSkin.play();
			//325 22
			var awardVo :MatchEndAwardVo = _data;
			_symbolSkin.addChild(openBoxMc);
			openBoxItem = new MatchAwardItem();
			openBoxItem.initUI(openBoxMc);
			openBoxMc.x = _symbolSkin["item_" + type].x;
			openBoxMc.y = _symbolSkin["item_" + type].y;
			openBoxItem.removeEvtListener();
			openBoxItem.select();
			i  = 0;
			while(i < 4)
			{
				MatchAwardItem(itemList[i]).removeEvtListener();
				i++;
			}
			this.removeEventListener(MouseEvent.CLICK,onBoxItemClickHandler);
			AppEventDispatcher.getInstance().dispatchEvent(new MatchEvent(MatchEvent.GET_AWARD,{matchtype:awardVo.matchtype,index:(type+1),rewardid:awardVo.rewardid}));
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.MATCH_USER_AWARD_RES,showRes);
			//TweenLite.delayedCall(.2,showRes);
		}
		/**
		 *显示结果 
		 */		
		public function showRes(e :NotifierEvent):void
		{
			var ret :int = int(e.params.ret);
			var pids :Array = e.params.pids;
			var awardVo :MatchEndAwardVo = _data;
			awardVo.pids = pids;
			if(ret == 0)
			{
				openBoxItem.data = awardVo.pids[type];
				//TweenLite.delayedCall(1,moveToBag);
				TweenLite.delayedCall(1.5,showOtherItem,[type]);
			}
			TweenLite.delayedCall(4,sureHandler);
		}
		/**
		 *显示其他的物品 
		 * 
		 */		
		private function showOtherItem(type :int):void
		{
			var i :int = 0;
			var j :int = 0;
			var awardVo :MatchEndAwardVo = _data;
			while(i < 4)
			{
				itemList[i].data = awardVo.pids[i];
				i ++;
			}
		}
		/**
		 * 箱子移动完成 
		 * @param pid
		 * 
		 */		
		private function boxMoveComplete(pid :int):void
		{
			openBoxItem.data = pid;
		}
		/**
		 *飞向背包 
		 */		
		private function moveToBag():void
		{
			var gPoint :Point ;
			var aDispt :DisplayObject = openBoxMc.iconContainer;
			gPoint = aDispt.localToGlobal(new Point(0,0));
			var bDispt :DisplayObject = openBoxItem.cloneItem();
			AnimationManager.moveToThing(gPoint,bDispt,CompGlobalDef.BagPoint,{scaleX:.5,scaleY:.5},1);
		}
		/**
		 * 确认
		 * @param e
		 * 
		 */		
		private function sureHandler(e:MouseEvent = null):void
		{
			destroyView();
		}
		/**
		 *删除事件 
		 * 
		 */		
		public function removeEvtListener():void
		{
			sureBtn.removeEventListener(MouseEvent.CLICK,sureHandler);
			this.removeEventListener(MouseEvent.CLICK,onBoxItemClickHandler);
			GlobalDef.GameStage.removeEventListener(Event.RESIZE,onResizeHandler);
			AppEventDispatcher.getInstance().removeEventListener(NotifierEvent.MATCH_USER_AWARD_RES,showRes);
		}
		/**
		 *销毁 
		 */		
		public function destroyView():void
		{
			TweenLite.killTweensOf(moveToBag);
			TweenLite.killTweensOf(showOtherItem);
			removeEvtListener();
			AnimationManager.remove();
			TweenLite.to(this,.3,{alpha:0,onComplete:fadeOutComplete});
			_data = null;
		}
		/**
		 *消失 
		 * 
		 */		
		override protected function fadeOutComplete():void
		{
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}